from apps.interface.pageConfig.baseViews import *
from apps.interface.client.dao import ClientDao
from apps.interface.advert.serializers import Advert, AdvertBaseSerializer
from apps.interface.pageConfig.data import minePageConfig

class PageConfigViewSet(BasePageConfigViewSet):

    def get_queryset(self):
        params = getRequestParams(self.request)
        _list = PageConfigDao().findAll().select_related('pagee').prefetch_related('clients')
        if params.get('lessee'):
        # if self.request.user.role.key == 'superAdmin' or self.request.user.role.key == 'admin':
        #     _list = _list
        # else:
            _clients = ClientDao().findAll().filter(lessee=params.get('lessee'))
            _list = PageConfigDao().findAll().filter(clients__in=_clients).select_related('pagee').prefetch_related('clients').distinct()
        else:
            _list = _list
        return _list.order_by('id')

    '''根据终端获取界面配置'''
    @action(methods=['post'], detail=False, authentication_classes = [], permission_classes = [])
    def getPageConfigByClientname(self, request):
        params = getRequestParams(request)
        _client = ClientDao().findAll().filter(clientname=params.get('clientname')).first()
        _pageConfigs = PageConfig.objects.filter(pagee__name=params.get('name'), clients__in=[_client])
        if _pageConfigs.count()==0:
            return Response(response_error('界面不存在'))
        return Response(response_success(PageConfigDetailSerializer(_pageConfigs.first()).data))

    
    '''更新信息'''
    @action(methods=['post'], detail=False)
    def updatePageConfig(self, request):
        params = getRequestParams(request)
        _advertList = params.get('advertList')
        if type(_advertList) == list:
            for advert in _advertList:
                if advert.get('id'):
                    serializers = AdvertBaseSerializer(Advert.objects.get(id=advert.get('id')), data=advert, partial=True)
                    if serializers.is_valid():
                        serializers.save()
                    else:
                        print(serializers.errors)
        PageConfigDao().update(params.get('pageConfigId'), params)
        return Response(response_success('更新成功'))
    
    '''获取个人中心配置数据'''
    @action(methods=['post'], detail=False, authentication_classes = [], permission_classes = [])
    def getMinePageConfig(self, request):
        params = getRequestParams(request)
        return Response(response_success(minePageConfig.config))

    '''获取个人中心私有配置数据'''
    @action(methods=['post'], detail=False)
    def getMinePageConfigForPrivate(self, request):
        params = getRequestParams(request)
        config = [
            {
                'title': '',
                'items': [
                    {
                        'title': "我的团队",
                        'icon': '/static/common/mine/duoshou.png',
                        'color': '#999',
                        'name': 'duoshou',
                        'size': '26',
                    }
                ]
            }
        ]
        if self.request.user.rank.grade >= 1:
            return Response(response_success(config))
        else:
            return Response(response_success([]))
